package student

import (
	"cqsx/classmates"
	"fmt"
	"os"
	"strconv"
	"time"
)

type person struct {
	Name       string
	sn         string
	Age        int
	height     float32
	address    string
	xuedli     string
	level      int
	fathername string
	// 钱包
	money float32
	fuqin *person
	muqin *person
}
type class struct {
	name         string
	nianji       string
	banzhang     person
	xuexiweiyuan person
	xuesheng     []person
}
type zhuanye struct {
	xizhuren person
	liebiao  []class
}
type xueyuan struct {
	yuanzhang      person
	zhuanyeLiebiao []zhuanye
}

type xuexiao struct {
	xiaozhang      person
	xueyuanliebiao []xueyuan
}

type country struct {
	name  string
	zhuxi person
}

type Student struct {
	// 继承于 person
	person
	xuehao  string
	chengji float32
	teacher *person
	Email   string
}
type YunStudent struct {
	// 继承于 student
	Student
	yunIP    string
	yunZhuji string
	yunFudao *person
}

func (s *Student) study() {

	fmt.Println(s.Name, "在学习")
}

func OOBDemo() {

	var lzl person
	lzl.Name = "梁铮凌"
	lzl.money = 200

	var qhd person
	qhd.money = 1000
	var xuyuan *person
	xuyuan = &lzl
	fmt.Println(xuyuan)

	fmt.Println("前 money", qhd.money)
	qhd.fahongbao(200, &lzl)
	fmt.Println("money", qhd.money)
	fmt.Println("lzl'money", lzl.money)

}

func (p *person) fahongbao(edu float32, mubiao ...*person) {
	p.money = p.money - edu*float32(len(mubiao))

	for i := 0; i < len(mubiao); i++ {
		mubiao[i].money += edu
	}
}

// 转账
func ZhuanZhang() {

	//当前登录的人，转账给第三方
	var targetName string
	fmt.Println("请输入你要转账的人的名字")
	fmt.Scanln(&targetName)

	//查找对应的人
	var targetList []*classmates.Classmate
	classmates.DB.Raw("SELECT*FROM classmates.classmates WHERE name=?", targetList).Scan(&targetList)
	fmt.Println("找到下面重名的人员，请选择学号进行确认")
	for _, person := range targetList {

		fmt.Println("sn", person.Sn, "name", person.Name, "ID", person.ID)

	}

	//提示用户选择要转入人的ID和金额
	var targetID int
	var edu float64
	fmt.Println("请输入你要转入的ID")
	fmt.Scanln(&targetID)
	fmt.Println("请输入你要转入的金额")
	fmt.Scanln(&edu)

	//当前人的钱减少
	classmates.LoginUser.Money -= float32(edu)
	//双方的新信息存入数据库
	classmates.DB.Save(classmates.LoginUser)
	classmates.DB.Exec("UPDATE classmates.classmates ste money=(money+?) where id=?", edu, targetID)
	fmt.Println("转账成功")
}

// 将每个学生的考勤记录写入对应学生文本文件
func SaveStudentRecords() {
	var allStudents []classmates.Classmate
	classmates.DB.Raw("SELECT*FROM classmates.classmates").Scan(&allStudents)
	for _, stu := range allStudents {
		mulu := "D:/go语言/" + strconv.Itoa(stu.ClassId)
		cuowu := os.MkdirAll(mulu, os.ModeDir)
		if cuowu != nil {
			fmt.Println("创建目录的时候出错，错误信息是：", cuowu)
			return
		}
		fmt.Println("创建的目录是：", mulu+"/"+stu.Sn+stu.Name+".txt")
		currentFile, err := os.Create(mulu + stu.Sn + stu.Name + ".txt")
		if err != nil {
			fmt.Println("创建失败的原因是：", err)
			return
		}
		fmt.Println("正在处理" + stu.Name + "的数据，请耐心等待...\n")
		var thisStudentRecord []classmates.Records
		classmates.DB.Raw("select*from classmates.records where sn=?", stu.Sn).Scan(&thisStudentRecord)
		currentFile.WriteString("面向对象程序设计的考勤记录\n")
		for _, records := range thisStudentRecord {
			currentFile.WriteString(records.Name + records.Createtime.String() + records.Comments + "\n")
		}
		currentFile.WriteString("以上就是该同学的所有考勤记录。等级时间是：" + time.Now().String())
		currentFile.Close()
	}

}
